﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.world {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;

    public class RoomMediator extends AbstractAreaMediator {

        private var _needsInventoryCheck:Dictionary;
		[Inject]
        public var sunModel:SunModel;
		[Inject]
        public var gameStateModel:GameStateModel;
		[Inject]
        public var tutorialModel:TutorialModel;
		[Inject]
        public var languageModel:LanguageModel;
		[Inject]
        public var letterModel:LetterModel;

        private static const AWAITING_DOCTOR:String = "AWAITING_DOCTOR";

        override public function onRegister():void{
            var _local1:ElementVO;
            super.onRegister();
            eventMap.mapListener(this.view, EventConstants.SNAP_IN, this.onSnapIn);
            eventMap.mapListener(this.view, EventConstants.SNAP_OUT, dispatch);
            eventMap.mapListener(this.view, ElementEvent.MOVE_ELEMENT, this.onMoveElement);
            eventMap.mapListener(eventDispatcher, EventConstants.HIGHLIGHT_ROOM, this.onHighlight);
            eventMap.mapListener(eventDispatcher, EventConstants.UPDATE_BUILD_MODE, this.onToggleCustomizing);
            eventMap.mapListener(eventDispatcher, EventConstants.ACTIVATE_ITEMS, this.onActivateItems);
            eventMap.mapListener(eventDispatcher, EventConstants.DEACTIVATE_ITEMS, this.onDeactivateItems);
            eventMap.mapListener(eventDispatcher, CustomizeEvent.ITEM_CLICKED, this.onItemClicked);
            eventMap.mapListener(eventDispatcher, EventConstants.REMOVE_SNAPAREAS, this.onRemoveSnapareas);
            eventMap.mapListener(eventDispatcher, EventConstants.ADD_ELEMENT, this.onAddElement);
            eventMap.mapListener(eventDispatcher, ElementEvent.MOVE_ELEMENT, this.onMovedElement);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_ELEMENT_REMOVED, this.onRemovedElement);
            eventMap.mapListener(eventDispatcher, EventConstants.ADD_ENVELOPE, this.addEnvelope);
            eventMap.mapListener(eventDispatcher, EventConstants.REMOVE_ENVELOPE, this.removeEnvelope);
            eventMap.mapListener(eventDispatcher, EventConstants.ENABLE_DARKEN_ROOM, this.onEnableDarkenRoom);
            eventMap.mapListener(eventDispatcher, EventConstants.DISABLE_DARKEN_ROOM, this.onDisabledarkenRoom);
            addContextListener(EventConstants.SHOW_HIRE_DOCTOR_BUTTON, this.onShowHireDoctorButton);
            addContextListener(EventConstants.SHOW_WELCOME_DOCTOR_BUTTON, this.onShowWelcomeDoctorButton);
            addContextListener(BuildingEvent.FACADE_CHANGED, this.facadeChanged);
            addContextListener(EventConstants.ON_OPEN_CUSTOMIZE_MENU, this.onOpenCustomizeMenu);
            addContextListener(EventConstants.ON_CLOSE_CUSTOMIZE_MENU, this.onCloseCustomizeMenu);
            addContextListener(EventConstants.ADD_HIRED_DOCTOR, this.onAddHireDoctor);
            addViewListener(EventConstants.ROOM_CLICKED, this.onRoomClicked);
            this._needsInventoryCheck = new Dictionary();
            this._needsInventoryCheck[GameConstants.REWARD_BATTERY] = true;
            this._needsInventoryCheck[GameConstants.REWARD_COFFEE] = true;
            this._needsInventoryCheck[GameConstants.REWARD_COFFEE_PACK] = true;
            for each (_local1 in this.vo.elements) {
                if (_local1.z > this.vo.maxZ){
                    this.vo.maxZ = _local1.z;
                };
                this.view.addElement(_local1);
            };
            this.view.initMachine(this.vo.machine.id, this.vo.machinePath, this.vo.machineView);
            this.view.setRewardPositions(this.vo.rewardStartPos, this.vo.rewardLandingXStart, this.vo.rewardLandingXEnd);
        }
        override protected function storeViewRectangle():void{
            this.vo.viewRectangle = this.view.rectangle;
        }
        private function onRoomClicked(_arg1:Event):void{
            var _local2:Point = new Point(this.view.mouseX, this.view.mouseY);
            var _local3:InfoEvent = new InfoEvent(EventConstants.ROOM_CLICKED, id, _local2);
            dispatch(_local3);
        }
        private function onCloseCustomizeMenu(_arg1:Event):void{
            this.view.setEnvelopesVisible(true);
        }
        private function onOpenCustomizeMenu(_arg1:Event):void{
            this.view.setEnvelopesVisible(false);
        }
        private function onActivateItems(_arg1:Event):void{
            this.view.isBuildMode = true;
        }
        private function onDeactivateItems(_arg1:Event):void{
            this.view.isBuildMode = false;
        }
        private function addEnvelope(_arg1:InfoEvent):void{
            var _local2:LetterVO;
            var _local3:MovieClip;
            if (id == _arg1.id){
                _local2 = LetterVO(_arg1.info);
                _local3 = this.view.addEnvelope(_local2.id, _local2.position.x, _local2.position.y);
                _local3.addEventListener(MouseEvent.CLICK, this.envelope_click);
                this.view.setEnvelopesVisible(!(this.gameStateModel.isFacadeVisible));
            };
        }
        private function envelope_click(_arg1:MouseEvent):void{
            var _local2:MovieClip = MovieClip(_arg1.currentTarget);
            _local2.removeEventListener(MouseEvent.CLICK, this.envelope_click);
            var _local3:String = _local2.name;
            this.view.removeEnvelope(_local3);
            var _local4:LetterVO = this.letterModel.getLetterById(_local3);
            dispatch(new InfoEvent(EventConstants.CREATE_POPUP, _local4.variant, {id:_local3}));
        }
        private function removeEnvelope(_arg1:InfoEvent):void{
            log("remove Envelope");
            var _local2:String = _arg1.id;
            this.view.removeEnvelope(_local2);
        }
        private function get view():RoomView{
            return ((viewComponent as RoomView));
        }
        private function get vo():IRoom{
            return ((_vo as IRoom));
        }
        private function onHighlight(_arg1:InfoEvent):void{
            if (id == _arg1.id){
                this.view.highlightRoom();
            };
        }
        private function onEnableDarkenRoom(_arg1:InfoEvent):void{
            if ((((id == _arg1.id)) && (!(userModel.isVisitFriendsMode())))){
                this.view.enableDarkenRoom();
            };
        }
        private function onDisabledarkenRoom(_arg1:InfoEvent):void{
            if (id == _arg1.id){
                this.view.disableDarkenRoom();
            };
        }
        private function onAddHireDoctor(_arg1:InfoEvent):void{
            if (id == _arg1.id){
                this.removeHireDoctorListener();
                this.vo.pendingDoctor = _arg1.info;
                this.setWelcomeDoctor();
            };
        }
        private function dispatchCreateDoctor(_arg1:Object):void{
            dispatch(new InfoEvent(EventConstants.CREATE_NEW_DOCTOR, null, _arg1));
        }
        private function onShowHireDoctorButton(_arg1:InfoEvent):void{
            var _local2:String;
            if (id == _arg1.id){
                this.reserveRoom();
                _local2 = this.languageModel.getTextByKey("hire_doctor_button");
                this.view.showHireDoctorButton(_local2);
                this.view.hireDoctorButton.addEventListener(MouseEvent.CLICK, this.onHireDoctorClick);
                this.addHireDoctorHoverListener();
                this.onEnableDarkenRoom(_arg1);
            };
        }
        private function reserveRoom():void{
            this.vo.reservedBy = AWAITING_DOCTOR;
        }
        private function unReserveRoom():void{
            this.vo.reservedBy = null;
        }
        private function onShowWelcomeDoctorButton(_arg1:InfoEvent):void{
            if (id == _arg1.id){
                this.reserveRoom();
                this.vo.pendingDoctor = _arg1.info;
                this.setWelcomeDoctor();
                this.onEnableDarkenRoom(_arg1);
            };
        }
        private function playHiredDoctorAnimation():void{
            var _local1:MovieClip = AssetsLibrary.getMovieClip("doctorHiredAnimation");
            this.view.addHiredDoctorAnimation(_local1);
            _local1.addEventListener(Event.ENTER_FRAME, this.onHiredAnimation);
            _local1.gotoAndStop(1);
        }
        private function onHiredAnimation(_arg1:Event):void{
            var _local2:MovieClip = (_arg1.target as MovieClip);
            if (_local2.currentFrame < _local2.totalFrames){
                _local2.gotoAndStop((_local2.currentFrame + 1));
            } else {
                _local2.removeEventListener(Event.ENTER_FRAME, this.onHiredAnimation);
                _local2.parent.removeChild(_local2);
                this.hireDoctorAnimationFinished();
            };
        }
        private function hireDoctorAnimationFinished():void{
            this.dispatchCreateDoctor(this.vo.pendingDoctor);
            dispatch(new InfoEvent(EventConstants.HANDLE_NEXT_IN_QUEUE, this.vo.type));
        }
        private function setWelcomeDoctor():void{
            var _local1:String = this.languageModel.getTextByKey("hire_doctor_welcome_button");
            var _local2:String = this.vo.pendingDoctor.pet_type;
            var _local3:String = this.languageModel.getTextByKey("doctors_doctorsbox_headline");
            this.view.showHireDoctorButton(_local1);
            this.view.addHireDoctorThumb(_local2, _local3);
            this.view.hireDoctorButton.addEventListener(MouseEvent.CLICK, this.onWelcomeDoctorClick);
            this.addHireDoctorHoverListener();
        }
        private function onWelcomeDoctorClick(_arg1:MouseEvent):void{
            dispatch(new InfoEvent(GameConstants.WELCOME_A_DOCTOR, this.vo.pendingDoctor.id));
            this.removeHireDoctorButton();
            this.playHiredDoctorAnimation();
            this.unReserveRoom();
            this.view.disableDarkenRoom();
        }
        private function removeHireDoctorButton():void{
            this.view.hideHireDoctorButton();
            this.removeHireDoctorListener();
        }
        private function removeHireDoctorListener():void{
            this.view.hireDoctorButton.removeEventListener(MouseEvent.CLICK, this.onHireDoctorClick);
            this.view.hireDoctorButton.removeEventListener(MouseEvent.CLICK, this.onWelcomeDoctorClick);
            this.view.hireDoctorButton.removeEventListener(MouseEvent.MOUSE_OVER, this.onHireDoctorOver);
            this.view.hireDoctorButton.removeEventListener(MouseEvent.MOUSE_OUT, this.onHireDoctorOut);
        }
        private function addHireDoctorHoverListener():void{
            this.view.hireDoctorButton.addEventListener(MouseEvent.MOUSE_OVER, this.onHireDoctorOver);
            this.view.hireDoctorButton.addEventListener(MouseEvent.MOUSE_OUT, this.onHireDoctorOut);
        }
        private function onHireDoctorOver(_arg1:MouseEvent):void{
            this.view.hireDoctorButton.gotoAndStop("hover");
        }
        private function onHireDoctorOut(_arg1:MouseEvent):void{
            this.view.hireDoctorButton.gotoAndStop("default");
        }
        private function onHireDoctorClick(_arg1:MouseEvent):void{
            dispatch(new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.HIRE_DOCTOR_POPUP, {roomId:id}));
        }
        private function onToggleCustomizing(_arg1:Event):void{
            this.view.isBuildMode = this.gameStateModel.isBuildMode;
        }
        private function onItemClicked(_arg1:CustomizeEvent):void{
            this.handleItemClicked(_arg1.destination);
        }
        private function handleItemClicked(_arg1:String):void{
            if ((this.vo is ICustomizable)){
                switch (_arg1){
                    case ShopItemVO.DESTINATION_FACADE:
                    case ShopItemVO.DESTINATION_WALL:
                    case ShopItemVO.DESTINATION_FLOOR:
                    case ShopItemVO.DESTINATION_CEILING:
                        this.view.initSnaparea();
                        break;
                    default:
                        l("e unhandled destination:", _arg1);
                };
            };
        }
        private function onRemovedElement(_arg1:InfoEvent):void{
            if (id == _arg1.id){
                this.view.removeElement((_arg1.info as ElementVO));
            };
        }
        private function onMovedElement(_arg1:ElementEvent):void{
            this.handleItemClicked(_arg1.destination);
        }
        private function onMoveElement(_arg1:ElementEvent):void{
            _arg1.roomId = id;
            dispatch(_arg1.clone());
        }
        private function onAddElement(_arg1:InfoEvent):void{
            if (id == _arg1.id){
                this.view.addElement(ElementVO(_arg1.info));
            };
        }
        private function onRemoveSnapareas(_arg1:Event):void{
            if ((this.vo is ICustomizable)){
                this.view.cleanup();
            };
        }
        private function onSnapIn(_arg1:SnapEvent):void{
            _arg1.id = id;
            dispatch(_arg1.clone());
        }
        private function facadeChanged(_arg1:Event):void{
            var _local2:Boolean = this.gameStateModel.isFacadeVisible;
            if (!this.gameStateModel.isBuildMode){
                this.view.setEnvelopesVisible(!(_local2));
            };
            ((this.vo.hasFacade) && (this.view.facadeVisible(_local2)));
            this.view.setDarkenedRoomVisible(!(_local2));
        }

    }
}//package net.wooga.pets.views.world 
